package com.ng.common.util;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import jakarta.servlet.http.HttpServletRequest;

@Slf4j
public class RequestParamUtil {

	/**
	 * 从 HttpServletRequest 中获取 JSON 参数
	 *
	 * @param request HttpServletRequest 对象
	 * @return 包含 JSON 参数的 Map
	 */
	public static JSONObject getRequestParams(HttpServletRequest request) {
		try (BufferedReader streamReader = new BufferedReader(
				new InputStreamReader(request.getInputStream(), "UTF-8"))) {
			StringBuilder responseStrBuilder = new StringBuilder();
			String inputStr;
			while ((inputStr = streamReader.readLine()) != null) {
				responseStrBuilder.append(inputStr);
			}
			// 使用泛型指定返回类型
			return JSON.parseObject(responseStrBuilder.toString(),
					new com.alibaba.fastjson.TypeReference<JSONObject>() {
					});
		} catch (Exception e) {
			// 记录异常信息
			log.error("Dbapi参数解析异常:{}", e.getMessage());
			return new JSONObject(); // 返回空的 Map
		}
	}
}